<?php

namespace App\Models;

class Cases extends Base
{
    protected $table = 'cases';

    const STATUS_1 = 1;
    const STATUS_2 = 2;
    const STATUS_3 = 3;

    const STATUS_TEXT = [
        self::STATUS_1 => '<span class="el-tag">正常</span>',
        self::STATUS_2 => '<span class="el-tag el-tag--danger">待审核</span>',
        self::STATUS_3 => '<span class="el-tag el-tag--info">未通过</span>',
    ];

    protected $fillable = [
        'id','store_id','title','thumb','summary','images','content','status','created_at','updated_at'
    ];

    protected $casts = ['images' => 'array'];

    //连表查询
    public function store(){//店铺
        return $this->belongsTo('App\Models\Stores', 'store_id', 'id');
    }
    public function cate_pid(){//一级分类
        return $this->belongsTo('App\Models\Categorys', 'cate_pid', 'id');
    }
    public function cate_id(){//二级分类
        return $this->belongsTo('App\Models\Categorys', 'cate_id', 'id');
    }

    /**
     * 获取列表
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function getList($params = [], $fields = ['*'], $limit = 10){
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params['kwd']) && $params['kwd']) {
                    $query ->where('name','like','%' . $params['kwd'] . '%');
                }
                if(isset($params['store_id']) && $params['store_id']) {
                    $query ->where('store_id',$params['store_id']);
                }
                if(isset($params['status']) && $params['status']) {
                    $query ->where('status',$params['status']);
                }
                if(isset($params['recommend']) && $params['recommend']) {
                    $query ->where('recommend',$params['recommend']);
                }
                if(isset($params['cid']) && $params['cid']) {
                    $query ->where('cate_id',$params['cid'])->orwhere('cate_pid',$params['cid']);
                }
                if(isset($params['store_id']) && $params['store_id']) {
                    $query ->where('store_id',$params['store_id']);
                }
                if(isset($params['start']) && $params['start']) {
                    $query ->whereDate('created_at','>=',$params['start']);
                }
                if(isset($params['end']) && $params['end']) {
                    $query ->whereDate('created_at','<=',$params['end']);
                }
            })->with(['store' => function ($query) {
                $query->select(['id','store_name'])->get();
            }])->with(['cate_id' => function ($query) {
                $query->select(['id','name'])->get();
            }])->with(['cate_pid' => function ($query) {
                $query->select(['id','name'])->get();
            }])->when($params, function ($query) use($params){
                if(isset($params['sort']) && $params['sort']) {
                    return $query->orderBy('view',$params['sort']);
                }
            })->where('status','<>',-1)
            ->orderBy('created_at', 'desc')
            ->paginate($limit);

        $this->stateToText($lists,[
            'status'=> self::STATUS_TEXT,
            'recommend'   => self::RECOMMEND_TEXT,
        ]);
        return $lists;
    }

//    /**
//     * 接口获取列表
//     * @param array $params 条件参数
//     * @param array $fields 查询的字段
//     * @param int $limit 分页数量
//     * @return mixed
//     */
//    protected function apiLists($params = [], $limit = 10, $fields = ['*']){
//        $params['status'] = 1;
//        $lists = $this->select($fields)
//            ->where(function ($query) use ($params) {
//                if(isset($params['status']) && $params['status']) {
//                    $query ->where('status',$params['status']);
//                }
//                if(isset($params['store_id']) && $params['store_id']) {
//                    $query ->where('store_id',$params['store_id']);
//                }
//            })->where('status','<>',-1)
//            ->orderBy('created_at', 'desc')->limit($limit)->get();
//        return $lists;
//    }

    /**
     * 所有数据
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function allLists($params = [], $fields = ['*'], $page = '', $limit = 10){
        $params['status'] = 1;
        if(isset($page)) {
            $lists = $this->select($fields)
                ->where(function ($query) use ($params) {
                    if(isset($params['status']) && $params['status']) {
                        $query ->where('status',$params['status']);
                    }
                    if(isset($params['store_id']) && $params['store_id']) {
                        $query ->where('store_id',$params['store_id']);
                    }
                })->where('status','<>',-1)
                ->orderBy('created_at', 'desc')->paginate($limit, null, null, $page);
        }else {
            $lists = $this->select($fields)
                ->where(function ($query) use ($params) {
                    if(isset($params['status']) && $params['status']) {
                        $query ->where('status',$params['status']);
                    }
                    if(isset($params['store_id']) && $params['store_id']) {
                        $query ->where('store_id',$params['store_id']);
                    }
                })->where('status','<>',-1)
                ->orderBy('created_at', 'desc')->limit($limit)->get();
        }
        return $lists;
    }

    /**
     * 根据id查询一条数据
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function details($params = [], $fields = ['*']){
        return $this->select($fields)->where($params)->where('status','<>',-1)->first();
    }

}
